package org.ligson.org.controller;

import lombok.extern.slf4j.Slf4j;
import org.ligson.org.client.UserClient;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/api/org")
@Slf4j
public class OrgController {
    private RestTemplate restTemplate;
    static AtomicInteger atomicInteger = new AtomicInteger(0);
    private UserClient userClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private AmqpTemplate amqpTemplate;

    public OrgController(RestTemplate restTemplate, UserClient userClient) {
        this.restTemplate = restTemplate;
        this.userClient = userClient;
    }

    @GetMapping("/test")
    public String test() {

        ExecutorService pool = Executors.newCachedThreadPool();
        for (int i = 0; i < 1000; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    String result = restTemplate.getForObject("http://scuser/scuser/api/user/test", String.class);
                    atomicInteger.incrementAndGet();
                    log.info("result:{},count:{}", result, atomicInteger.get());
                }
            });

        }
        return "OK";
    }

    //@Retryable(maxAttempts = 2)
    @GetMapping("/retry")
    public String retry() {
        String result = restTemplate.getForObject("http://scuser/scuser/api/user/test", String.class);
        atomicInteger.incrementAndGet();
        log.info("result:{},count:{}", result, atomicInteger.get());
        return "OK";
    }


    //@Retryable(maxAttempts = 2)
    @GetMapping("/retry2")
    public String retry2() {
        String result = userClient.sctest();
        atomicInteger.incrementAndGet();
        log.info("result:{},count:{}", result, atomicInteger.get());
        return "OK";
    }

    static int count = 0;

    @GetMapping("/rabbit")
    public String rabbit() {
        Message message = MessageBuilder.withBody(("hello" + count).getBytes()).setHeader("ip", "111").build();
        //amqpTemplate.send("test", "demo", message);
        // amqpTemplate.send("demo", message);
        // rabbitTemplate.send("demo", message);
        rabbitTemplate.convertAndSend("defaultExchange", "defaultQueue", message);


        Message sms = MessageBuilder.withBody(("sms" + count).getBytes()).setHeader("ip", "111").build();

        //rabbitTemplate.convertAndSend("smsExchange", "smsQueue", sms);
        rabbitTemplate.convertAndSend("smsExchange", null, sms);

        count++;
        return "ok";
    }


}
